// pages/meal_All/meal_All.js
const app = getApp()
const getdata = require("../../tool/GetDate.js");
const util = require("../../utils/util.js");
var currentPage = 0;
var listData = [];
var paramData = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mealList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    currentPage = 1;
    listData = [];
    let param = {};
    param.userID = app.globalData.myUserData.userID;
    param.hallName = "";
    param.startDate = util.formatDateDiff(-7);
    param.endDate = util.formatDateDiff(7);
    param.currentPage = currentPage;
    param.districtID = app.globalData.myUserData.districtID;
    paramData = param;
    console.log(param);
    let data = JSON.stringify(param);
    
    this.allMealReport(data);
  },

  allMealReport:function(data){
    let that = this;
    getdata.postdata({
      url: "gbt/Meal/MealReportSelectDate",
      data: data,
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          let array = res.root.data;
          let mealItem = {};
          for (let index = 0; index < array.length; index++) {
            mealItem.title = array[index].name;
            mealItem.hallName = array[index].hallName;
            mealItem.mealDateTitle = "报餐日期";
            mealItem.mealDateValue = array[index].date.replace("T00:00:00", "");
            mealItem.mealTitle = "餐点";
            mealItem.mealValue = array[index].mealName;
            mealItem.numTitle = "份数";
            mealItem.numValue = array[index].qty;
            mealItem.amount = array[index].amount;
            mealItem.currency = "元";
            listData.push(mealItem);
            mealItem = {};
          }
          console.log(listData);
          that.setData({
            mealList: listData,
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    currentPage++;
    paramData.currentPage = currentPage;
    let data = JSON.stringify(paramData);
    this.allMealReport(data);
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function() {
    
  // },

  onDef: function(e) {
    console.log(e.detail);
    listData = [];
    paramData = JSON.parse(e.detail);
    this.allMealReport(paramData);
  },

  onConfirm: function(e) {
    console.log(e.detail);
    listData = [];
    paramData = JSON.parse(e.detail);
    this.allMealReport(paramData);
  },
})